package cn.sinata.wanglin.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * description： 几秒前，几分钟前，几小时前，昨天；
 * Created by kaihongwu on 2016/12/26 上午 11:37
 */
public class RelativeDateFormat {
    private static final long ONE_MINUTE = 60000L;
    private static final long ONE_HOUR = 3600000L;
    private static final String ONE_SECOND_AGO = "秒前";
    private static final String ONE_MINUTE_AGO = "分钟前";
    private static final String ONE_HOUR_AGO = "小时前";

    public static String format(String date) {
        long cdate= Long.parseLong(date);
        long delta = new Date().getTime() - cdate;
        if (delta < 1L * ONE_MINUTE) {
            long seconds = toSeconds(delta);
            return (seconds <= 0 ? 1 : seconds) + ONE_SECOND_AGO;
        }
        if (delta < 45L * ONE_MINUTE) {
            long minutes = toMinutes(delta);
            return (minutes <= 0 ? 1 : minutes) + ONE_MINUTE_AGO;
        }
        if (delta < 24L * ONE_HOUR) {
            long hours = toHours(delta);
            return (hours <= 0 ? 1 : hours) + ONE_HOUR_AGO;
        }
        if (delta < 48L * ONE_HOUR) {
            return "昨天";
        }
        return  getCurrentTimeMMDD(cdate);
    }

    private static long toSeconds(long date) {
        return date / 1000L;
    }

    private static long toMinutes(long date) {
        return toSeconds(date) / 60L;
    }

    private static long toHours(long date) {
        return toMinutes(date) / 60L;
    }

    private static long toDays(long date) {
        return toHours(date) / 24L;
    }

    private static long toMonths(long date) {
        return toDays(date) / 30L;
    }

    private static long toYears(long date) {
        return toMonths(date) / 365L;
    }
    /**
     * 时间戳转换日期格式
     * @param timestamp 单位毫秒
     * @return
     */
    public static String getCurrentTimeMMDD(long timestamp) {
        SimpleDateFormat f = new SimpleDateFormat("MM-dd", Locale.CHINA);
        return f.format(new Date(timestamp));
    }
    /**
     * 时间戳转换日期格式
     * @param timestamp 单位秒
     * @return
     */
    public static String getCurrentTimeMMDDHHMM(String timestamp) {
        long cdate= Long.parseLong(timestamp);
        SimpleDateFormat f = new SimpleDateFormat("MM-dd HH:mm",Locale.CHINA);
        return f.format(new Date(cdate));
    }
    /**
     * 时间戳转换日期格式
     * @param timestamp 单位秒
     * @return
     */
    public static String getCurrentTimeMMDDHHMM(long timestamp) {
        SimpleDateFormat f = new SimpleDateFormat("MM-dd HH:mm",Locale.CHINA);
        return f.format(new Date(timestamp));

    }
    /**
     * 时间戳转换日期格式
     * @param timestamp 单位秒
     * @return
     */
    public static String getTimeYMDHMS(String timestamp) {
        long cdate= Long.parseLong(timestamp);
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA);
        return f.format(new Date(cdate));

    }
    /**
     * 时间戳转换日期格式
     * @param timestamp 单位秒
     * @return
     */
    public static String getTimeYMDHMS(long timestamp) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA);
        return f.format(new Date(timestamp));

    }

    /**
     * 日期转时间戳
     * @param time
     * @return
     */
    public static long getSimpleTime(String time) {

        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",Locale.CHINA);

        Date date = null;
        try {
            date = f.parse(time);

            return date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return System.currentTimeMillis();
        }
    }
    public static String format(long date) {
        long delta = new Date().getTime() - date;
        if (delta < 1L * ONE_MINUTE) {
            long seconds = toSeconds(delta);
            return (seconds <= 0 ? 1 : seconds) + ONE_SECOND_AGO;
        }
        if (delta < 45L * ONE_MINUTE) {
            long minutes = toMinutes(delta);
            return (minutes <= 0 ? 1 : minutes) + ONE_MINUTE_AGO;
        }
        if (delta < 24L * ONE_HOUR) {
            long hours = toHours(delta);
            return (hours <= 0 ? 1 : hours) + ONE_HOUR_AGO;
        }
        if (delta < 48L * ONE_HOUR) {
            return "昨天";
        }
        return  getCurrentTimeMMDD(date);
    }
    public static boolean isLowerCurrent(long time){
        if (time <System.currentTimeMillis()) {
            return true;
        } else {
            return false;
        }
    }
}

